<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use think\Db;
use app\admin\model\ReceptionUnit;
use app\admin\logic\ZHXY;
use think\Cache;


class Index extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function index()
    {
        echo '';
        exit();
        return $this->view->fetch();
    }

    public function news()
    {
        $newslist = [];
        return jsonp(['newslist' => $newslist, 'new' => count($newslist), 'url' => 'https://www.fastadmin.net?ref=news']);
    }

    public function setcard(){
        $list = Db::name('outsideschool')->where('LENGTH(card_id)!=18')->order('id desc')->select();
        foreach ($list as $k => $v){
            $isset = Db::name('outsideschool')->where('nickname',$v['nickname'])->where('tel',$v['tel'])->where('LENGTH(card_id)=18')->find();
            if(!empty($isset)){
                echo $v['nickname']."###".$v['tel']."<br/>";
            }
        }
        die();
    }

    // 早上八点给  20:00-7:00发企业微信
    public function sendwechat(){
        $list = Db::name('outsideschool')->where('is_schoolfriend=1 and is_sendmessage=1')->select();
        foreach ($list as $key => $value)
        {
           if($value['is_check']==0){
                $reception_info = ReceptionUnit::where(['id'=>$value['reception_id']])->find();
                if($reception_info -> sid){
                    $sid_arr = explode(',',$reception_info->sid);
                    foreach($sid_arr as $k1 => $v1){
                        send_wechat_work_msg([$v1], '<a href="https://yqsb.qau.edu.cn/index/outsideschool/view">您有一份校外人员'.$value['applier_name'].'提交的入校预约申请需要审批。</a>');
                    }
                    $sf_sid_arr = explode(',',$reception_info->sf_sid);
                    foreach($sf_sid_arr as $k2 => $v2){
                        send_wechat_work_msg([$v2], '<a href="https://yqsb.qau.edu.cn/index/outsideschool/view">您有一份校外人员'.$value['applier_name'].'提交的入校预约申请需要审批。</a>');
                    }
                    // 社会合作处
                    if($reception_info->id!=60){
                        // 校友申请给，于宁老师，张正明老师发送企业微信
                        send_wechat_work_msg([200201092], '<a href="https://yqsb.qau.edu.cn/index/outsideschool/view">您有一份校外人员'.$value['applier_name'].'提交的入校预约申请需要审批。</a>');
                        send_wechat_work_msg([199501036], '<a href="https://yqsb.qau.edu.cn/index/outsideschool/view">您有一份校外人员'.$value['applier_name'].'提交的入校预约申请需要审批。</a>');
                    }
                }
                
           }
           Db::name('outsideschool')->where('id',$value['id'])->update(['is_sendmessage'=>0]);
        }
    }

    /**
     * 定时更新超时未处理的待办任务
     * [updatezhxy description]
     * @return [type] [description]
     */
    public function updatezhxy(){
        $datalist = Db::name('outsideschool')->where(['is_outside_zhxy'=>['in',[0,1]]])->limit(1100)->order('id asc')->where('admission_endtime<'.date('Ymd'))->where('outside_zhxy_json is not null and outside_zhxy_json <>""')->select();
        foreach ($datalist as $y => $v)
        {
            if(!empty($v['outside_zhxy_json'])){                
                $outside = json_decode($v['outside_zhxy_json'],true);
                $app_id  = $outside['app_id'];
                $inst_id = $outside['inst_id'];
                $task_id = $outside['task_id'];

                if($v['is_check']<>0){
                    $check_cn = $v['check_cn'];
                    $check_sid = $v['check_sid'];
                }else{
                    $unit = Db::name('reception_unit')->where('id',$v['reception_id'])->find();
                    if(!empty($unit)){
                        $cn = explode(',',$unit['nickname']);
                        $sid = explode(',',$unit['sid']);
                        $check_cn = $cn[0];
                        $check_sid = $sid[0];
                    }else{
                        $check_cn = '超期未审批';
                        $check_sid = '123456789';
                    }
                }

                $arr['inst_data'] = [[
                    "inst_id"=>$inst_id,
                    "app_id"=>$app_id,
                    "creator"=>$v['applier_id'],
                    "created"=>date('Y-m-d H:i:s',$v['create_time']),
                    "finished"=>date('Y-m-d H:i:s'),
                    "percent"=>100,
                    "status"=>2,
                    'node_id'=>[$v['id']],
                    "node_name"=>[$check_cn],
                    "name"=>"校外人员(".$v['nickname'].")进校申请",
                    "form_url_view"=>"https://yqsb.qau.edu.cn/index/outsideschool/view",
                    "form_mobile_url_view"=>"https://yqsb.qau.edu.cn/index/outsideschool/view",
                    "thirdparty_id"=>config('zhxy.outside_thirdparty_id'),
                    "process_pic_url"=>"",
                    "conversion_column"=>['app_id','inst_id','node_id','task_id']
                ]];
                $arr['task_data'] = [[
                    "inst_id"=>$inst_id,
                    "app_id"=>$app_id,
                    "task_id"=>$task_id,
                    "node_id"=>$v['id'],
                    "status"=>3,
                    "created"=>date('Y-m-d H:i:s',$v['create_time']),
                    "finished"=>date('Y-m-d H:i:s'),
                    "form_url"=>"",
                    "form_url_view"=>"https://yqsb.qau.edu.cn/index/outsideschool/view",
                    "form_mobile_url"=>"https://yqsb.qau.edu.cn/index/outsideschool/view",
                    "form_mobile_url_view"=>"https://yqsb.qau.edu.cn/index/outsideschool/view",
                    "process_pic_url"=>"",
                    "thirdparty_id"=>config('zhxy.outside_thirdparty_id'),
                    "node_name"=>$check_cn,
                    "number"=>[$check_sid],
                    "conversion_column"=>['app_id','inst_id','node_id','task_id']
                ]];
                $url  = config('zhxy.push-task');
                $data2 =  $this->send_zhxy_msg($url,$arr,'outside');
//                echo "<pre>";
//                echo $v['id']."<br>";
//                print_r($data2);
                if($data2[0] && $data2[1]=='发送成功'){
                    // 2为超时处理的，不是自动审批
                    Db::name('outsideschool')->where(['id'=>$v['id']])->update(['is_outside_zhxy'=>2]);
                }
            }
        }
        echo "完成";
        exit();
    }
    
    public function updatezhxy_finish(){
        $datalist = Db::name('outsideschool')->where(['is_outside_zhxy'=>1,'is_check'=>1])->limit(1500)->where('admission_endtime>='.date('Ymd'))->where('outside_zhxy_json is not null and outside_zhxy_json <>""')->order('id asc')->select();
        foreach ($datalist as $y => $v)
        {
            if(!empty($v['outside_zhxy_json'])){                
                $outside = json_decode($v['outside_zhxy_json'],true);
                $app_id  = $outside['app_id'];
                $inst_id = $outside['inst_id'];
                $task_id = $outside['task_id'];

                $check_cn = $v['check_cn'];
                $check_sid = $v['check_sid'];

                $arr['inst_data'] = [[
                    "inst_id"=>$inst_id,
                    "app_id"=>$app_id,
                    "creator"=>$v['applier_id'],
                    "created"=>date('Y-m-d H:i:s',$v['create_time']),
                    "finished"=>date('Y-m-d H:i:s'),
                    "percent"=>100,
                    "status"=>2,
                    'node_id'=>[$v['id']],
                    "node_name"=>[$check_cn],
                    "name"=>"校外人员(".$v['nickname'].")进校申请",
                    "form_url_view"=>"",
                    "form_mobile_url_view"=>"",
                    "thirdparty_id"=>config('zhxy.outside_thirdparty_id'),
                    "process_pic_url"=>"",
                    "conversion_column"=>['app_id','inst_id','node_id','task_id']
                ]];
                $arr['task_data'] = [[
                    "inst_id"=>$inst_id,
                    "app_id"=>$app_id,
                    "task_id"=>$task_id,
                    "node_id"=>$v['id'],
                    "status"=>3,
                    "created"=>date('Y-m-d H:i:s',$v['create_time']),
                    "finished"=>date('Y-m-d H:i:s'),
                    "form_url"=>"",
                    "form_url_view"=>"",
                    "form_mobile_url"=>"",
                    "form_mobile_url_view"=>"",
                    "process_pic_url"=>"",
                    "thirdparty_id"=>config('zhxy.outside_thirdparty_id'),
                    "node_name"=>$check_cn,
                    "number"=>[$check_sid],
                    "conversion_column"=>['app_id','inst_id','node_id','task_id']
                ]];
                $url  = config('zhxy.push-task');
                $data2 =  $this->send_zhxy_msg($url,$arr,'outside');
                if($data2[0] && $data2[1]=='发送成功'){
                    // 2为超时处理的，不是自动审批
                    Db::name('outsideschool')->where(['id'=>$v['id']])->update(['is_outside_zhxy'=>2]);
                }
            }

        }
    }
  	
    public function updatezhxy_new(){
        $datalist = Db::name('outsideschool')->where(['is_outside_zhxy'=>1,'is_check'=>['>',0]])->limit(1500)->where('outside_zhxy_json is not null and outside_zhxy_json <>""')->order('id desc')->select();
        foreach ($datalist as $y => $v)
        {
            if(!empty($v['outside_zhxy_json'])){                
                $outside = json_decode($v['outside_zhxy_json'],true);
                $app_id  = $outside['app_id'];
                $inst_id = $outside['inst_id'];
                $task_id = $outside['task_id'];

                if($v['is_check']<>0){
                    $check_cn = $v['check_cn'];
                    $check_sid = $v['check_sid'];
                }else{
                    $check_cn = '超期未审批';
                    $check_sid = '123456789';
                }

                $arr['inst_data'] = [[
                    "inst_id"=>$inst_id,
                    "app_id"=>$app_id,
                    "creator"=>$v['applier_id'],
                    "created"=>date('Y-m-d H:i:s',$v['create_time']),
                    "finished"=>date('Y-m-d H:i:s'),
                    "percent"=>100,
                    "status"=>2,
                    'node_id'=>[$v['id']],
                    "node_name"=>[$check_cn],
                    "name"=>"校外人员(".$v['nickname'].")进校申请",
                    "form_url_view"=>"https://yqsb.qau.edu.cn/index/outsideschool/view",
                    "form_mobile_url_view"=>"https://yqsb.qau.edu.cn/index/outsideschool/view",
                    "thirdparty_id"=>config('zhxy.outside_thirdparty_id'),
                    "process_pic_url"=>"",
                    "conversion_column"=>['app_id','inst_id','node_id','task_id']
                ]];
                $arr['task_data'] = [[
                    "inst_id"=>$inst_id,
                    "app_id"=>$app_id,
                    "task_id"=>$task_id,
                    "node_id"=>$v['id'],
                    "status"=>3,
                    "created"=>date('Y-m-d H:i:s',$v['create_time']),
                    "finished"=>date('Y-m-d H:i:s'),
                    "form_url"=>"",
                    "form_url_view"=>"https://yqsb.qau.edu.cn/index/outsideschool/view",
                    "form_mobile_url"=>"https://yqsb.qau.edu.cn/index/outsideschool/view",
                    "form_mobile_url_view"=>"https://yqsb.qau.edu.cn/index/outsideschool/view",
                    "process_pic_url"=>"",
                    "thirdparty_id"=>config('zhxy.outside_thirdparty_id'),
                    "node_name"=>$check_cn,
                    "number"=>[$check_sid],
                    "conversion_column"=>['app_id','inst_id','node_id','task_id']
                ]];
                $url  = config('zhxy.push-task');
                $data2 =  $this->send_zhxy_msg($url,$arr,'outside');
                if($data2[0] && $data2[1]=='发送成功'){
                    // 2为超时处理的，不是自动审批
                    Db::name('outsideschool')->where(['id'=>$v['id']])->update(['is_outside_zhxy'=>3]);
                }
            }
        }
        echo "完成";
        exit();
    }
	
    //已审核还在待办
    public function updatezhxy_checked(){
        $datalist = Db::name('outsideschool')->where(['is_outside_zhxy'=>0,'is_check'=>['>',0]])->limit(1500)->where('outside_zhxy_json is not null and outside_zhxy_json <>""')->order('id desc')->select();
        foreach ($datalist as $y => $v)
        {
            if(!empty($v['outside_zhxy_json'])){                
                $outside = json_decode($v['outside_zhxy_json'],true);
                $app_id  = $outside['app_id'];
                $inst_id = $outside['inst_id'];
                $task_id = $outside['task_id'];

                if($v['is_check']<>0){
                    $check_cn = $v['check_cn'];
                    $check_sid = $v['check_sid'];
                }else{
                    $check_cn = '超期未审批';
                    $check_sid = '123456789';
                }

                $arr['inst_data'] = [[
                    "inst_id"=>$inst_id,
                    "app_id"=>$app_id,
                    "creator"=>$v['applier_id'],
                    "created"=>date('Y-m-d H:i:s',$v['create_time']),
                    "finished"=>date('Y-m-d H:i:s'),
                    "percent"=>100,
                    "status"=>2,
                    'node_id'=>[$v['id']],
                    "node_name"=>[$check_cn],
                    "name"=>"校外人员(".$v['nickname'].")进校申请",
                    "form_url_view"=>"https://yqsb.qau.edu.cn/index/outsideschool/view",
                    "form_mobile_url_view"=>"https://yqsb.qau.edu.cn/index/outsideschool/view",
                    "thirdparty_id"=>config('zhxy.outside_thirdparty_id'),
                    "process_pic_url"=>"",
                    "conversion_column"=>['app_id','inst_id','node_id','task_id']
                ]];
                $arr['task_data'] = [[
                    "inst_id"=>$inst_id,
                    "app_id"=>$app_id,
                    "task_id"=>$task_id,
                    "node_id"=>$v['id'],
                    "status"=>3,
                    "created"=>date('Y-m-d H:i:s',$v['create_time']),
                    "finished"=>date('Y-m-d H:i:s'),
                    "form_url"=>"",
                    "form_url_view"=>"https://yqsb.qau.edu.cn/index/outsideschool/view",
                    "form_mobile_url"=>"https://yqsb.qau.edu.cn/index/outsideschool/view",
                    "form_mobile_url_view"=>"https://yqsb.qau.edu.cn/index/outsideschool/view",
                    "process_pic_url"=>"",
                    "thirdparty_id"=>config('zhxy.outside_thirdparty_id'),
                    "node_name"=>$check_cn,
                    "number"=>[$check_sid],
                    "conversion_column"=>['app_id','inst_id','node_id','task_id']
                ]];
                $url  = config('zhxy.push-task');
                $data2 =  $this->send_zhxy_msg($url,$arr,'outside');
                if($data2[0] && $data2[1]=='发送成功'){
                    // 2为超时处理的，不是自动审批
                    Db::name('outsideschool')->where(['id'=>$v['id']])->update(['is_outside_zhxy'=>3]);
                }
            }
        }
	    unset($v);
        echo "完成";
        exit();
    }

    /**
     * 补充审核人名称
     * @return void
     */
    public function bccheck_cn()
    {
        $datalist = Db::name('outsideschool')->where(['is_check'=>['>',0]])->where('check_cn is null and check_sid<>""')->order('id desc')->select();
        foreach ($datalist as $y => $v)
        {
            $cn = Db::name('outsideschool')->where('check_cn<>""')->where(['check_sid' => $v['check_sid']])->value('check_cn');
            if(empty($cn)){
                $cn = Db::name('teacher')->where(['tel' => $v['check_sid'],'type'=>1])->value('nickname');
            }
            Db::name('outsideschool')->where(['id'=>$v['id']])->update(['check_cn'=>$cn]);
        }
        unset($v);
        die('完成');
    }
    /**
     * 
     * [send_zhxy_msg description]
     * @param  [type] $url       [description]
     * @param  [type] $post_data [description]
     * @param  [type] $type      [description]
     * @return [type]            [description]
     */
    private function send_zhxy_msg($url, $post_data,$type = null)
    {
        $token         = (new \app\admin\logic\zhxy\GetAccessToken())->get_token($type);
        if(!$token){
            return 'token is null';
        }
        $domain  = config('zhxy.school_url');
        $urlFull = $domain . $url . '?access_token=' . $token;
        $data = json_post($urlFull, $post_data);
        // halt($data);
        $res = json_decode($data, true);
        if ($res['e'] == 0) {
            return [ true, '发送成功', $res ];
        }
        return [ false, $res['m'], $res ];
    }


    /**
     * 处理超时未处理的待办任务 公用方法
     * [timeout description]
     * @return [type] [description]
     */
    protected function deletetask($task_id = null,$thirdparty_id = null){
        $token         = (new \app\admin\logic\zhxy\GetAccessToken())->get_token('outside');
        if(!$token){
            return 'token is null';
        }
        $domain      = config('zhxy.school_url');
        $url         = config('zhxy.delete-task');
        $m_shool_url = $domain . $url;
        //构建接口URL
        $url = $m_shool_url . "?access_token={$token}&task_id={$task_id}&thirdparty_id={$thirdparty_id}";
        // $result = file_get_contents($url);
        // halt($result);
        //请求接口(推荐使用curl)
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_TIMEOUT, 5);

        // 要求结果为字符串且输出到屏幕上
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        // 运行curl
        $data = curl_exec($curl);
        curl_close($curl);
        $res = json_decode($data, true);
        return $res;
    }

    /**
     * 处理超时未处理的待办任务
     * [timeout description]
     * @return [type] [description]
     */
    public function timeout(){
        $list = Db::name('timeout_task')
        ->where('states=0 and thirdparty_id=15')
        ->where('ids<117')
        // ->where("created not like '%".date('Y-m-d')."%'")
        ->order('ids asc')->select();
        foreach ($list as $key => $value){
            // 先判断是否存在，然后进行
            
            // // 如果存在则修改为驳回或者其他
            // $arr['inst_data'] = [[
            //     "inst_id"=>$inst_id,
            //     "app_id"=>$app_id,
            //     "creator"=>$v['applier_id'],
            //     "created"=>date('Y-m-d H:i:s',$v['create_time']),
            //     "finished"=>date('Y-m-d H:i:s'),
            //     "percent"=>100,
            //     "status"=>2,
            //     'node_id'=>[$v['id']],
            //     "node_name"=>[$check_cn],
            //     "name"=>"校外人员(".$v['nickname'].")进校申请",
            //     "form_url_view"=>"",
            //     "form_mobile_url_view"=>"",
            //     "thirdparty_id"=>config('zhxy.outside_thirdparty_id'),
            //     "process_pic_url"=>"",
            //     "conversion_column"=>['app_id','inst_id','node_id','task_id']
            // ]];
            // $arr['task_data'] = [[
            //     "inst_id"=>$inst_id,
            //     "app_id"=>$app_id,
            //     "task_id"=>$task_id,
            //     "node_id"=>$v['id'],
            //     "status"=>3,
            //     "created"=>date('Y-m-d H:i:s',$v['create_time']),
            //     "finished"=>date('Y-m-d H:i:s'),
            //     "form_url"=>"",
            //     "form_url_view"=>"",
            //     "form_mobile_url"=>"",
            //     "form_mobile_url_view"=>"",
            //     "process_pic_url"=>"",
            //     "thirdparty_id"=>config('zhxy.outside_thirdparty_id'),
            //     "node_name"=>$check_cn,
            //     "number"=>[$check_sid],
            //     "conversion_column"=>['app_id','inst_id','node_id','task_id']
            // ]];
            // $url  = config('zhxy.push-task');
            // $data2 =  $this->send_zhxy_msg($url,$arr,'outside');
            
            // 如果不存在则删除
            $ret = $this->deletetask($value['task_id'],$value['thirdparty_id']);
            if($ret['e']==0 && $res['d']='操作成功'){
                Db::name('timeout_task')
                ->where(['task_id'=>$value['task_id'],'thirdparty_id'=>$value['thirdparty_id']])
                ->update(['states'=>1,'finished'=>date('Y-m-d H:i:s')]);
            }
        }
        unset($value);
        exit('完成');
    }

    /**
     * 
     * [zhxy description]
     * @return [type] [description]
     */
    public function zhxy(){
        $info = Db::name('reception_unit')->where('id',20)->find();
        $info_arr = explode(',',$info['sid']);
        // foreach ($info_arr as $key => $value) {
        //    $arr2[] = $value;
        // }
        // print_r($arr);
        // exit();

        $name_arr = explode(',',$info['nickname']);
        foreach ($name_arr as $key => $value) {
           $arr2[] = $value."审批";
        }

        // $outside_zhxy = [
        //     'inst_id'=>'outside647ff226840f5729320',
        //     'app_id'=>'outside647ff226840fb401728',
        //     'task_id'=>'outside647ff226840fc561770',
        // ];
        // echo json_encode($outside_zhxy);exit();

        $inst_id = uniqid('outside').mt_rand(100000, 999999);
        $app_id = uniqid('outside').mt_rand(100000, 999999);
        $appname = '校外人员入校';
        $task_id = uniqid('outside').mt_rand(100000, 999999);

        $created_key = 'zhxy_create_' . (string)$app_id;
        $created     = Cache::get($created_key);
        if (!$created) {
            // 没有创建app
            $re = $this->push_apps($app_id,$appname);
          
            if ($re[0]) {
                Cache::set($created_key, 1, 3600 * 48);
            }
           // echo Cache::get($created_key)."###<br/>";
        }
        
        

        $href_url = 'https://yqsb.qau.edu.cn/index/outsideschool/view';
        $arr['inst_data'] = [[
            "inst_id"=>$inst_id,
            "app_id"=>$app_id,
            "creator"=>199701043,
            "created"=>date('Y-m-d H:i:s'),
            "finished"=>"",
            "percent"=>0,
            "status"=>0,
            'node_id'=>["88254"],
            "node_name"=>$arr2,
            "name"=>"校外人员(王同林)进校申请",
            "form_url_view"=>$href_url,
            "form_mobile_url_view"=>$href_url,
            "thirdparty_id"=>config('zhxy.outside_thirdparty_id'),
            "process_pic_url"=>"",
            "conversion_column"=>['app_id',
            'inst_id',
            'node_id',
            'task_id']
        ]];
        $arr['task_data'] = [[
            "inst_id"=>$inst_id,
            "app_id"=>$app_id,
            "task_id"=>$task_id,
            "node_id"=>88254,
            "status"=>0,
            "created"=>date('Y-m-d H:i:s'),
            "finished"=>"",
            "form_url"=>$href_url,
            "form_url_view"=>$href_url,
            "form_mobile_url"=>$href_url,
            "form_mobile_url_view"=>$href_url,
            "process_pic_url"=>"",
            "thirdparty_id"=>config('zhxy.outside_thirdparty_id'),
            "node_name"=>$info['nickname']."审批",
            "number"=>$info_arr,
            "conversion_column"=>['app_id',
            'inst_id',
            'node_id',
            'task_id']
        ]];
        echo json_encode($arr);
        exit();
        $url  = config('zhxy.push-task');
        $data =  $this->send_msg($url,$arr,'outside');
        if($data[0] && $data[1]=='发送成功'){
            $outside_zhxy = [
                'inst_id'=>$inst_id,
                'app_id'=>$app_id,
                'task_id'=>$task_id,
            ];
            Db::name('outsideschool')->where()->update(['outside_zhxy_json'=>json_encode($outside_zhxy)]);
        }
        halt($data);



        
        // $inst_id = uniqid('xy').mt_rand(100000, 999999);
        // $app_id = uniqid('xy').mt_rand(100000, 999999);
        // $appname = '校友入校';
        // $task_id = uniqid('xy').mt_rand(100000, 999999);


        // $created_key = 'zhxy_create_' . (string)$app_id;
        // $created     = Cache::get($created_key);
        // if (!$created) {
        //     // 没有创建app
        //     $re = $this->push_apps($app_id,$appname);
          
        //     if ($re[0]) {
        //         Cache::set($created_key, 1, 3600 * 48);
        //     }
        //    // echo Cache::get($created_key)."###<br/>";
        // }
        
        // $outside_zhxy = [
        //     'inst_id'=>$inst_id,
        //     'app_id'=>$app_id,
        //     'task_id'=>$task_id,
        // ];

        // $href_url = 'https://yqsb.qau.edu.cn/index/outsideschool/view';
        // $arr['inst_data'] = [[
        //     "inst_id"=>$inst_id,
        //     "app_id"=>$app_id,
        //     "creator"=>83925,
        //     "created"=>date('Y-m-d H:i:s'),
        //     "finished"=>"",
        //     "percent"=>0,
        //     "status"=>0,
        //     'node_id'=>["88073"],
        //     "node_name"=>["郝老师审批"],
        //     "name"=>"隋洁文(校友)进校申请",
        //     "form_url_view"=>$href_url,
        //     "form_mobile_url_view"=>$href_url,
        //     "thirdparty_id"=>config('zhxy.friend_thirdparty_id'),
        //     "process_pic_url"=>"",
        //     "conversion_column"=>['app_id',
        //     'inst_id',
        //     'node_id',
        //     'task_id']
        // ]];
        // $arr['task_data'] = [[
        //     "inst_id"=>$inst_id,
        //     "app_id"=>$app_id,
        //     "task_id"=>$task_id,
        //     "node_id"=>88073,
        //     "status"=>0,
        //     "created"=>date('Y-m-d H:i:s'),
        //     "finished"=>"",
        //     "form_url"=>$href_url,
        //     "form_url_view"=>$href_url,
        //     "form_mobile_url"=>$href_url,
        //     "form_mobile_url_view"=>$href_url,
        //     "process_pic_url"=>"",
        //     "thirdparty_id"=>config('zhxy.friend_thirdparty_id'),
        //     "node_name"=>"郝老师审批",
        //     "number"=>["200401061"],
        //     "conversion_column"=>['app_id',
        //     'inst_id',
        //     'node_id',
        //     'task_id']
        // ]];
        // $url  = config('zhxy.push-task');
        // $data =  $this->send_msg($url,$arr,'friend');
        // halt($data);
    }

    public function zhxy1(){
        $outside_zhxy_json = '{"inst_id":"outside647ff226840f5729320","app_id":"outside647ff226840fb401728","task_id":"outside647ff226840fc561770"}';
        $outside = json_decode($outside_zhxy_json,true);
        
        $app_id = $outside['app_id'];
        $inst_id = $outside['inst_id'];
        $task_id = $outside['task_id'];

        $href_url = '';
        $arr['inst_data'] = [[
            "inst_id"=>$inst_id,
            "app_id"=>$app_id,
            "creator"=>199701043,
            "created"=>date('Y-m-d H:i:s','1686106552'),
            "finished"=>date('Y-m-d H:i:s'),
            "percent"=>100,
            "status"=>2,
            'node_id'=>["88254"],
            "node_name"=>["郝老师审批"],
            "name"=>"校外人员(王同林)进校申请",
            "form_url_view"=>$href_url,
            "form_mobile_url_view"=>$href_url,
            "thirdparty_id"=>config('zhxy.outside_thirdparty_id'),
            "process_pic_url"=>"",
            "conversion_column"=>['app_id',
            'inst_id',
            'node_id',
            'task_id']
        ]];
        $arr['task_data'] = [[
            "inst_id"=>$inst_id,
            "app_id"=>$app_id,
            "task_id"=>$task_id,
            "node_id"=>88254,
            "status"=>3,
            "created"=>date('Y-m-d H:i:s','1686106552'),
            "finished"=>date('Y-m-d H:i:s'),
            "form_url"=>$href_url,
            "form_url_view"=>$href_url,
            "form_mobile_url"=>$href_url,
            "form_mobile_url_view"=>$href_url,
            "process_pic_url"=>"",
            "thirdparty_id"=>config('zhxy.outside_thirdparty_id'),
            "node_name"=>"郝老师审批",
            "number"=>["200401061"],
            "conversion_column"=>['app_id',
            'inst_id',
            'node_id',
            'task_id']
        ]];
        $url  = config('zhxy.push-task');
        $data =  $this->send_msg($url,$arr,'outside');
        halt($data);
        if($data[0] && $data[1]=='发送成功'){
            Db::name('outsideschool')->where(['id'])->update(['outside_zhxy_json'=>json_encode($outside_zhxy)]);
        }

    }
    public function push_apps($appid, $appname)
    {
        $url        = config('zhxy')['push-apps'];
        $conversion = true;
        $data       = [
            'conversion' => $conversion,
            'apps'       => [
                [
                    'id'            => $appid,
                    'name'          => $appname,
                    'thirdparty_id' => config('zhxy.friend_thirdparty_id'),
                ]
            ],
        ];
        $re = $this->send_msg($url, $data,'friend');
        if (!$re[0]) {
            return 'error';
            // Logger::error("error", [ $url, $data, $re ]);
        }

        return $re;
    }


    private function send_msg($url, $post_data,$type = null): array
    {
        $token         = (new \app\admin\logic\zhxy\GetAccessToken())->get_token($type);
        if(!$token){
            return 'token is null';
        }
        $domain  = config('zhxy.school_url');
        $urlFull = $domain . $url . '?access_token=' . $token;
        // Logger::debug("taskapi send ", [ $url, $post_data ]);
        // if(!ZHXY::getIsSend()){
        //     Logger::debug("taskapi send - no");

        //     return [ true, '发送成功', [] ];
        // }
        $data = $this->json_post($urlFull, $post_data);
        // halt($data);

        $res = json_decode($data, true);
        // Logger::debug("taskapi return  ", [ $data, $res ]);
//        dump($res);
        if ($res['e'] == 0) {
            return [ true, '发送成功', $res ];
        }

        return [ false, $res['m'], $res ];
    }


    /**
     * PHP发送Json对象数据
     *
     * @param $url 请求url
     * @param $data 发送的json字符串/数组
     * @return array
     */
    private function json_post($url, $data = NULL)
    {

        $curl = curl_init();

        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        if (!$data) {
            return 'data is null';
        }
        if (is_array($data)) {
            $data = json_encode($data);
        }
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_TIMEOUT, 5);

        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json; charset=utf-8',
            'Content-Length:' . strlen($data),
            'Cache-Control: no-cache',
            'Pragma: no-cache'
        ));
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $res     = curl_exec($curl);
        $errorno = curl_errno($curl);
        if ($errorno) {
            return $errorno;
        }
        curl_close($curl);

        return $res;

    }



}


